package com.glodon.paas.job.monitor.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.job.model.AlarmConfig;
import com.glodon.paas.job.model.AlarmInfo;
import com.glodon.paas.job.monitor.service.AlarmService;
import com.glodon.paas.job.util.DateFormatUtil;
import com.glodon.paas.job.util.JsonUtil;

@Controller
public class AlarmController {

	private static final Logger	logger	= LoggerFactory.getLogger(AlarmController.class);

	@Autowired
	private AlarmService		alarmService;

	@RequestMapping(value = "/alarm", method = RequestMethod.GET)
	public String getAlarm(Map model) throws Exception {
		return "alarm";
	}

	@RequestMapping(value = "/alarm/alarmInfo", method = RequestMethod.GET)
	public void getAlarmInfo(@RequestParam("fromNum") int fromNum, @RequestParam("pageNum") int pageNum, Map model)
			throws Exception {
		List<AlarmInfo> list = alarmService.getAlarmInfoForPage(fromNum, pageNum);
		List<Map<String, String>> status = new ArrayList<Map<String, String>>();
		for (AlarmInfo alarmInfo : list) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("alarmTime", DateFormatUtil.format(alarmInfo.getCreateTime()));
			map.put("isNotified", alarmInfo.isNotified() ? "Y" : "N");
			map.put("alarmInfo", alarmInfo.getAlarmDetail());
			map.put("updateTime", DateFormatUtil.format(alarmInfo.getUpdateTime()));
			status.add(map);
		}
		model.put("status", status);
	}

	@RequestMapping(value = "/alarm/alarmConfig", method = RequestMethod.POST)
	public void saveAlarmConfig(@RequestBody String formString, ModelMap model) throws Exception {
		AlarmConfig alarmConfig = JsonUtil.jsonToObject(formString, AlarmConfig.class);
		alarmService.processAlarmConfig(alarmConfig);
		model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
	}

	@RequestMapping(value = "/alarm/alarmConfig", method = RequestMethod.GET)
	public @ResponseBody
	String getAlarmConfig() throws Exception {
		AlarmConfig alarmConfig = alarmService.getConfig();
		return JsonUtil.objectToJson(alarmConfig);
	}

	@RequestMapping(value = "/alarm/healthCheck", method = RequestMethod.POST)
	public void checkServerHealth(@RequestBody String formString, ModelMap model) throws Exception {
		AlarmConfig alarmConfig = JsonUtil.jsonToObject(formString, AlarmConfig.class);
		boolean flag = alarmService.checkServerHealth(alarmConfig);
		if (flag) {
			model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
		} else {
			model.addAttribute(StringConst.RESULT, StringConst.ERROR);
		}
	}

	public AlarmService getAlarmService() {
		return alarmService;
	}

	public void setAlarmService(AlarmService alarmService) {
		this.alarmService = alarmService;
	}

}
